package com.esri.android.map;

import android.content.Context;
import android.util.DisplayMetrics;
import android.view.MotionEvent;
import android.view.View;
import android.view.WindowManager;
import com.esri.android.map.MapGestureDetector;
import com.esri.core.geometry.Envelope;
import com.esri.core.geometry.Point;
import com.github.mikephil.charting.utils.Utils;
import java.util.Timer;
import java.util.TimerTask;

/* loaded from: classes.dex */
public class MapOnTouchListener implements View.OnTouchListener, MapGestureDetector.OnGestureListener {
    public static final int PINCH_ZOOM_TOLERANECE_INDEGREE = 60;
    public static final int SENSITIVITY_TOLERANCE_INPIXEL = 5;
    private static final float h = -0.25f;
    private static final float i = 0.25f;
    Timer a;
    private final MapGestureDetector b;
    private final MapView c;
    private float p;
    private float q;
    private double s;
    private float t;
    private float u;
    private boolean y;
    private float d = Float.NaN;
    private float e = 1.0f;
    private boolean f = false;
    private boolean g = false;
    private double j = Double.NaN;
    private boolean k = false;
    private boolean l = false;
    private boolean m = false;
    private float n = Float.NaN;
    private float o = Float.NaN;
    private double r = Double.NaN;
    private boolean v = false;
    private int w = 70;
    private int x = 150;

    private float a(float f, float f2, float f3) {
        return f < f2 ? f2 : f > f3 ? f3 : f;
    }

    /* loaded from: classes.dex */
    private class a extends TimerTask {
        private a() {
        }

        @Override // java.util.TimerTask, java.lang.Runnable
        public void run() {
            MapOnTouchListener.this.c.a(MapOnTouchListener.this.e);
        }
    }

    public MapOnTouchListener(Context context, MapView mapView) {
        this.b = new MapGestureDetector(context, this);
        this.c = mapView;
        a(context);
    }

    private void a(Context context) {
        WindowManager windowManager = (WindowManager) context.getSystemService("window");
        DisplayMetrics displayMetrics = new DisplayMetrics();
        windowManager.getDefaultDisplay().getMetrics(displayMetrics);
        this.u = windowManager.getDefaultDisplay().getHeight();
        this.t = windowManager.getDefaultDisplay().getWidth();
        if (displayMetrics.densityDpi == 320) {
            this.w *= 2;
        }
    }

    @Override // android.view.View.OnTouchListener
    public boolean onTouch(View view, MotionEvent motionEvent) {
        return this.b.onTouchEvent(motionEvent);
    }

    @Override // com.esri.android.map.MapGestureDetector.OnGestureListener
    public boolean onDragPointerMove(MotionEvent motionEvent, MotionEvent motionEvent2) {
        this.c.a(true);
        if (this.c.q & this.v) {
            this.c.a(motionEvent2.getX(), motionEvent2.getY());
            if (this.c.r) {
                Point center = this.c.getCenter();
                Envelope envelope = new Envelope();
                this.c.getExtent().queryEnvelope(envelope);
                double width = envelope.getWidth();
                double d = this.x;
                Double.isNaN(d);
                double d2 = width / d;
                double height = envelope.getHeight();
                double d3 = this.x;
                Double.isNaN(d3);
                double d4 = height / d3;
                if (this.t - motionEvent2.getX() >= this.w) {
                    d2 = motionEvent2.getX() < ((float) this.w) ? -d2 : 0.0d;
                }
                if (this.u - motionEvent2.getY() < this.w) {
                    d4 = -d4;
                } else if (motionEvent2.getY() >= this.w) {
                    d4 = 0.0d;
                }
                if (d2 != Utils.DOUBLE_EPSILON || d4 != Utils.DOUBLE_EPSILON) {
                    this.c.centerAt(new Point(center.getX() + d2, center.getY() + d4), false);
                }
                return true;
            }
        } else if (this.y) {
            CalloutPopupWindow.onDragPointerMove(this.c, motionEvent2);
        } else {
            this.c.a(motionEvent.getX(), motionEvent.getY(), motionEvent2.getX(), motionEvent2.getY(), false);
        }
        return true;
    }

    @Override // com.esri.android.map.MapGestureDetector.OnGestureListener
    public boolean onDragPointerUp(MotionEvent motionEvent, MotionEvent motionEvent2) {
        this.c.a(false);
        if (this.c.q && this.v) {
            this.c.c();
            this.v = false;
            return true;
        } else if (this.y) {
            CalloutPopupWindow.onDragPointerUp(this.c, motionEvent2);
            this.y = false;
            return true;
        } else if (this.c.u == null) {
            this.c.a(motionEvent.getX(), motionEvent.getY(), motionEvent2.getX(), motionEvent2.getY(), true);
            return true;
        } else {
            this.c.u.prePointerUp(motionEvent.getX(), motionEvent.getY(), motionEvent2.getX(), motionEvent2.getY());
            this.c.a(motionEvent.getX(), motionEvent.getY(), motionEvent2.getX(), motionEvent2.getY(), true);
            this.c.u.postPointerUp(motionEvent.getX(), motionEvent.getY(), motionEvent2.getX(), motionEvent2.getY());
            return true;
        }
    }

    @Override // com.esri.android.map.MapGestureDetector.OnGestureListener
    public boolean onPinchPointersDown(MotionEvent motionEvent) {
        b();
        this.c.a(motionEvent.getX(0), motionEvent.getY(0), motionEvent.getX(1), motionEvent.getY(1));
        this.c.a(true);
        this.j = Double.NaN;
        this.d = Float.NaN;
        this.n = motionEvent.getX(0);
        this.o = motionEvent.getY(0);
        this.p = motionEvent.getX(1);
        float y = motionEvent.getY(1);
        this.q = y;
        double atan2 = Math.atan2(y - this.o, this.p - this.n);
        this.r = atan2;
        this.l = false;
        double degrees = Math.toDegrees(atan2);
        this.s = degrees;
        this.j = degrees;
        float f = this.q;
        float f2 = this.o;
        float f3 = (f - f2) * (f - f2);
        float f4 = this.p;
        float f5 = this.n;
        this.d = (float) Math.sqrt(f3 + ((f4 - f5) * (f4 - f5)));
        return true;
    }

    @Override // com.esri.android.map.MapGestureDetector.OnGestureListener
    public boolean onPinchPointersMove(MotionEvent motionEvent) {
        float x = motionEvent.getX(0);
        float y = motionEvent.getY(0);
        float x2 = motionEvent.getX(1);
        float y2 = motionEvent.getY(1);
        if (!this.l) {
            this.m = a(x, y, x2, y2);
        }
        this.n = x;
        this.o = y;
        this.p = x2;
        this.q = y2;
        if (this.l) {
            double d = Utils.DOUBLE_EPSILON;
            if (!this.m) {
                d = b(x, y, x2, y2);
            }
            double d2 = d;
            this.c.a(x, y, x2, y2, c(x, y, x2, y2), d2, !this.m);
        } else {
            double degrees = Math.toDegrees(Math.atan2(y2 - y, x2 - x));
            this.s = degrees;
            this.j = degrees;
            float f = this.q;
            float f2 = this.o;
            float f3 = (f - f2) * (f - f2);
            float f4 = this.p;
            float f5 = this.n;
            this.d = (float) Math.sqrt(f3 + ((f4 - f5) * (f4 - f5)));
        }
        return true;
    }

    @Override // com.esri.android.map.MapGestureDetector.OnGestureListener
    public boolean onPinchPointersUp(MotionEvent motionEvent) {
        this.c.a(false);
        float x = motionEvent.getX(0);
        float y = motionEvent.getY(0);
        float x2 = motionEvent.getX(1);
        float y2 = motionEvent.getY(1);
        this.c.a(x, y, x2, y2, c(x, y, x2, y2));
        this.d = Float.NaN;
        this.j = Double.NaN;
        CalloutPopupWindow.a(this.c);
        c();
        if (this.c.q) {
            this.c.c();
            this.v = false;
        }
        return true;
    }

    private boolean a(float f, float f2, float f3, float f4) {
        float f5 = this.o;
        float f6 = (f2 - f5) * (f2 - f5);
        float f7 = this.n;
        int sqrt = (int) Math.sqrt(f6 + ((f - f7) * (f - f7)));
        float f8 = this.q;
        float f9 = (f4 - f8) * (f4 - f8);
        float f10 = this.p;
        int sqrt2 = (int) Math.sqrt(f9 + ((f3 - f10) * (f3 - f10)));
        if (sqrt >= 5 || sqrt2 >= 5) {
            double degrees = Math.toDegrees(Math.atan2(f2 - this.o, f - this.n));
            if (degrees < Utils.DOUBLE_EPSILON) {
                degrees += 180.0d;
            }
            double degrees2 = Math.toDegrees(Math.atan2(f4 - this.q, f3 - this.p));
            if (degrees2 < Utils.DOUBLE_EPSILON) {
                degrees2 += 180.0d;
            }
            double d = this.s;
            if (d < Utils.DOUBLE_EPSILON) {
                this.s = d + 180.0d;
            }
            if (sqrt == 0 && Math.abs(degrees2 - this.s) < 60.0d) {
                this.l = true;
                return true;
            } else if (sqrt2 == 0 && Math.abs(degrees - this.s) < 60.0d) {
                this.l = true;
                return true;
            } else if (Math.abs(degrees2 - this.s) < 60.0d && Math.abs(degrees - this.s) < 60.0d) {
                this.l = true;
                return true;
            } else {
                this.l = true;
                return false;
            }
        }
        return false;
    }

    private double b(float f, float f2, float f3, float f4) {
        double degrees = Math.toDegrees(Math.atan2(f4 - f2, f3 - f));
        double d = !Double.isNaN(this.j) ? this.j - degrees : Utils.DOUBLE_EPSILON;
        this.j = degrees;
        return d;
    }

    private float c(float f, float f2, float f3, float f4) {
        float abs = Math.abs(f3 - f);
        float abs2 = Math.abs(f4 - f2);
        float sqrt = (float) Math.sqrt((abs * abs) + (abs2 * abs2));
        float f5 = !Float.isNaN(this.d) ? sqrt / this.d : 1.0f;
        this.d = sqrt;
        return f5;
    }

    @Override // com.esri.android.map.MapGestureDetector.OnGestureListener
    public boolean onDoubleTap(MotionEvent motionEvent) {
        this.c.a(false);
        this.c.b(motionEvent.getX(), motionEvent.getY(), true);
        return true;
    }

    @Override // com.esri.android.map.MapGestureDetector.OnGestureListener
    public void onMultiPointersSingleTap(MotionEvent motionEvent) {
        this.c.a(false);
        this.c.zoomout();
    }

    @Override // com.esri.android.map.MapGestureDetector.OnGestureListener
    public void onLongPress(MotionEvent motionEvent) {
        this.y = false;
        if (this.c.v == null || !this.c.v.onLongPress(motionEvent.getX(), motionEvent.getY())) {
            if (this.c.q) {
                this.v = true;
                this.c.a(motionEvent.getX(), motionEvent.getY());
            } else if (CalloutPopupWindow.onLongPress(motionEvent)) {
                this.y = true;
            }
        }
    }

    @Override // com.esri.android.map.MapGestureDetector.OnGestureListener
    public boolean onLongPressUp(MotionEvent motionEvent) {
        if (this.c.q) {
            this.c.c();
            this.v = false;
            return true;
        }
        return true;
    }

    @Override // com.esri.android.map.MapGestureDetector.OnGestureListener
    public boolean onSingleTap(MotionEvent motionEvent) {
        if (this.c.x != null) {
            this.c.x.onSingleTap(motionEvent.getX(), motionEvent.getY());
            return true;
        }
        return true;
    }

    @Override // com.esri.android.map.MapGestureDetector.OnGestureListener
    public boolean onFling(MotionEvent motionEvent, MotionEvent motionEvent2, float f, float f2) {
        this.c.a(false);
        this.c.a(motionEvent, motionEvent2, f, f2, true);
        return true;
    }

    @Override // com.esri.android.map.MapGestureDetector.OnGestureListener
    public boolean onDoubleTapDrag(MotionEvent motionEvent, MotionEvent motionEvent2) {
        MapView mapView = this.c;
        if (mapView != null && mapView.isAllowOneFingerZoom()) {
            float y = (motionEvent2.getY(0) - motionEvent.getY(0)) / this.u;
            if (Math.abs(y) > 0.02f) {
                this.e = (a(y, h, 0.25f) / 10.0f) + 1.0f;
                if (!this.f) {
                    this.f = true;
                    this.g = true;
                    b();
                    this.c.a(true);
                    if (this.a == null) {
                        this.a = new Timer();
                    }
                    this.a.schedule(new a(), 0L, 16L);
                }
            } else {
                a();
            }
        }
        return false;
    }

    @Override // com.esri.android.map.MapGestureDetector.OnGestureListener
    public boolean onDoubleTapDragUp(MotionEvent motionEvent) {
        a();
        if (!this.g) {
            return onDoubleTap(motionEvent);
        }
        this.g = false;
        return true;
    }

    private void a() {
        Timer timer = this.a;
        if (timer != null) {
            timer.cancel();
            this.a = null;
        }
        this.f = false;
        this.c.a(false);
        c();
    }

    private void b() {
        if (this.c.getCallout().isShowing()) {
            this.c.getCallout().hide();
            this.k = true;
        }
    }

    private void c() {
        if (this.k) {
            this.c.getCallout().show();
            this.k = false;
        }
    }
}
